const SFCTemplateCompileResults = require('@vue/compiler-sfc')

function relEl(el, slotList) {
    if (el.tag === 'slot') {
        // 解析查询组件
        const slotInfo = {
            name: '',
            props: []
        }
        const props = el.props
        // 获取插槽名称
        const nameProp = props.find(item => item.name === 'name')
        let slotName = 'default'
        if (nameProp) {
            slotName = nameProp.value.content
        }
        slotInfo.name = slotName
        const otherProps = props.filter(item => item.name !== 'name')
        if (otherProps && otherProps.length > 0) {
            for (let otherProp of otherProps) {
                slotInfo.props.push({
                    name: otherProp.exp.content
                })
            }
        }
        slotList.push(slotInfo)
    }
    const children = el.children
    if (children && children.length > 0) {
        for (let child of children) {
            relEl(child, slotList)
        }
    }
}

/**
 * 解析插槽
 * @param {SFCTemplateCompileResults} template vue解析后的Template
 * @return {{
 *     [string]: {
 *         name: string,
 *         props: {
 *             name: string
 *         }[]
 *     }
 * }|null}
 */
function relSlot(template) {
    if (!template.ast) return null
    const slotList = []
    const children = template.ast.children
    if (children && children.length > 0) {
        for (let child of children) {
            relEl(child, slotList)
        }
    }
    return slotList
}

exports.relSlot = relSlot;
